# -*- coding: utf-8 -*-
import time
import pytz
import datetime


def utc_to_local(utc_time_str, utc_format='%Y-%m-%dT%H:%M:%S'):
    """
    utc_to_local
    :param utc_time_str:
    :param utc_format:
    :return:
    """
    local_tz = pytz.timezone('Asia/Chongqing')
    local_format = "%Y-%m-%d %H:%M:%S"
    utc_dt = datetime.datetime.strptime(utc_time_str, utc_format)
    local_dt = utc_dt.replace(tzinfo=pytz.utc).astimezone(local_tz)
    time_str = local_dt.strftime(local_format)
    return time_str


def est_to_local(est_time_str, est_format='%b %d, %Y %I:%M %p EST'):
    """
    est_to_local
    :param est_time_str:
    :param est_format:
    :return:
    """
    est_tz = pytz.timezone('EST')  # 标注时间的时区
    local_tz = pytz.timezone('Asia/Chongqing')  # 北京时区
    local_format = "%Y-%m-%d %H:%M:%S"  # 所需要的时间打印格式
    est_dt = datetime.datetime.strptime(est_time_str, est_format)
    local_dt = est_dt.replace(tzinfo=est_tz).astimezone(local_tz)  # 将原有时区换成需要的
    time_str = local_dt.strftime(local_format)
    return time_str


def transfer_date(stamp):
    """
    将时间戳转换为date
    :param stamp:
    :return:
    """
    date_obj = datetime.datetime.fromtimestamp(int(stamp) / 1000, None)
    date_str = date_obj.strftime("%Y-%m-%d")
    return date_str


def datetime_to_timestamp(datetime_obj):
    """
    将本地(local) datetime 格式的时间 (含毫秒) 转为毫秒时间戳
    :param datetime_obj: 2019-12-27 11:35:06.241086    datetime.datetime.now()
    :return: 13 位毫秒时间戳  1577417706241
    """
    local_timestamp = int(time.mktime(datetime_obj.timetuple()) * 1000 + datetime_obj.microsecond / 1000)
    return local_timestamp


if __name__ == '__main__':
    print(utc_to_local('2019-08-05T11:26:06'))
